using System.Collections.Generic;
using ElasticsearchCRUD.Model;
using ElasticsearchCRUD.Utils;

namespace ElasticsearchCRUD.ContextAddDeleteUpdate.IndexModel.SettingsModel.Filters
{
	public class CjkBigramTokenFilter : AnalysisFilterBase
	{
		private bool _outputUnigrams;
		private bool _outputUnigramsSet;
		private bool _ignoreScriptsSet;
		private List<string> _ignoreScripts;

		/// <summary>
		/// The cjk_bigram token filter forms bigrams out of the CJK terms that are generated by the standard tokenizer or the icu_tokenizer (see ICU Analysis Plugin).
		/// By default, when a CJK character has no adjacent characters to form a bigram, it is output in unigram form. If you always want to output both unigrams and bigrams, 
		/// set the output_unigrams flag to true. This can be used for a combined unigram+bigram approach
		/// Bigrams are generated for characters in han, hiragana, katakana and hangul, but bigrams can be disabled for particular scripts with the ignore_scripts parameter. 
		/// All non-CJK input is passed through unmodified.
		/// </summary>
		/// <param name="name">name for the custom filter</param>
		public CjkBigramTokenFilter(string name)
		{
			AnalyzerSet = true;
			Name = name.ToLower();
			Type = DefaultTokenFilters.CjkBigram;
		}

		/// <summary>
		/// ignore_scripts
		/// </summary>
		public List<string> IgnoreScripts
		{
			get { return _ignoreScripts; }
			set
			{
				_ignoreScripts = value;
				_ignoreScriptsSet = true;
			}
		}

		/// <summary>
		/// output_unigrams
		/// </summary>
		public bool OutputUnigrams
		{
			get { return _outputUnigrams; }
			set
			{
				_outputUnigrams = value;
				_outputUnigramsSet = true;
			}
		}

		public override void WriteJson(ElasticsearchCrudJsonWriter elasticsearchCrudJsonWriter)
		{
			base.WriteJsonBase(elasticsearchCrudJsonWriter, WriteValues);
		}

		private void WriteValues(ElasticsearchCrudJsonWriter elasticsearchCrudJsonWriter)
		{
			JsonHelper.WriteValue("type", Type, elasticsearchCrudJsonWriter);
			JsonHelper.WriteListValue("ignore_scripts", _ignoreScripts, elasticsearchCrudJsonWriter, _ignoreScriptsSet);
			JsonHelper.WriteValue("output_unigrams", _outputUnigrams, elasticsearchCrudJsonWriter, _outputUnigramsSet);			
		}
	}
}